"""
Case Type   : 资源池化集群系统内部使用工具
Case Name   : 修改备份日志配置->查看配置
Create At   : 2024/04/19
Owner       : @kyleze
Description :
    1.主机上初始化目录
    2.主机上添加备份实例
    3.修改备份日志配置
    4.查看配置
Expect      :
    1.成功
    2.成功
    3.成功
    4.配置修改成功
History     :
"""

import os
import unittest
import json

from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH

from yat.test import Node
from yat.test import macro


@unittest.skipIf(
    "DMS" not in Common().get_sh_result(
    Node("PrimaryDbUser"), f"source {macro.DB_ENV_PATH}; "
    f"cm_ctl query -Cvidp"), "非资源池化环境不执行")
class SharedStorageGsProbackup(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f"-----{os.path.basename(__file__)} start-----")
        self.constant = Constant()
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.pri_com = Common('PrimaryDbUser')
        self.pri_node = Node(node='PrimaryDbUser')
        self.database = self.pri_node.db_name
        self.port = self.pri_node.db_port
        self.parent_path = os.path.dirname(macro.DB_INSTANCE_PATH)
        self.backup_path = os.path.join(self.parent_path,
                                        "backupset/backup_dir_027")
        self.instance_name = 'probackup_027'
        self.primary_script = os.path.join(
            macro.SCRIPTS_PATH, "sharestorage_probackup_primary_restore.sh")
        self.standby_script = os.path.join(
            macro.SCRIPTS_PATH, "sharestorage_probackup_standby_restore.sh")
        self.target_path = os.path.join(macro.DB_BACKUP_PATH, 'probackup_005')
        self.log.info("-----检查数据库状态是否正常-----")
        status = self.primary_sh.get_db_cluster_status()
        self.assertTrue("Degraded" in status or "Normal" in status)
        cmd = f'source {macro.DB_ENV_PATH};' \
              f'cat $DSS_HOME/cfg/dss_vg_conf.ini |cut -d ":" -f 1'
        vgname_infor = self.pri_node.sh(cmd).result().strip()
        self.vgname = "+" + vgname_infor.split('\n')[0].strip() + \
                      ",+" + vgname_infor.split('\n')[1].strip()
        self.socket_path = self.pri_com.show_param("ss_dss_conn_path")

    def test_gs_pro_backup(self):
        step = '--step1:主机上初始化目录 expect:成功--'
        self.log.info(step)
        result = self.primary_sh.exec_pro_backup_init(self.backup_path)
        self.log.info(result)
        self.assertTrue(result, "执行失败:" + step)

        step = '--step2:主机上添加备份实例 expect:成功--'
        self.log.info(step)
        result = self.primary_sh.exec_pro_back_add(
            self.backup_path, self.instance_name,
            f"--enable-dss --instance-id 0 --vgname='{self.vgname}'")
        self.log.info(result)
        self.assertTrue(result, "执行失败:" + step)

        step = '--step3:修改备份日志配置 expect:成功--'
        self.log.info(step)
        result = self.primary_sh.exec_probackup_cmd(
            'set-config', self.backup_path, self.instance_name,
            f"--log-level-console=warning --log-level-file=info"
            f"--log-filename=gs_probackup-%Y-%m-%d_%H:%N:%s.log"
            f"--error-log-filename=gs_probackup_error.log")
        self.log.info(result)
        self.assertIn('', result, "执行失败:" + step)

        step = '--step4:查看配置 expect:配置修改成功--'
        self.log.info(step)
        result = self.primary_sh.exec_probackup_cmd(
            'show-config', self.backup_path, self.instance_name,
            '--format=json')
        self.log.info(result)
        config_msg = json.loads(result)
        self.assertEqual('WARNING', config_msg['log-level-console'],
                         "执行失败:" + step)
        self.assertEqual('INFO', config_msg['log-level-file'],
                         "执行失败:" + step)
        self.assertEqual('gs_probackup-%Y-%m-%d_%H:%N:%s.log',
                         config_msg['log-filename'], "执行失败:" + step)
        self.assertEqual('gs_probackup_error.log',
                         config_msg['error-log-filename'], "执行失败:" + step)

    def tearDown(self):
        self.log.info('----清理环境----')
        clear_cmd = f"rm -rf {self.backup_path}"
        self.log.info(clear_cmd)
        clear_msg = self.pri_node.sh(clear_cmd).result()
        self.log.info(clear_msg)
        self.assertIn('', clear_msg, '删除目录失败')
        self.log.info(f"-----{os.path.basename(__file__)} end-----")
